package fragments;

import java.util.List;

import com.evaluity.R;

import logic.Answer;
import logic.Question;

import views.QuestionAdapter;

import activities.QuestionsList;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;

public class QuestionsListFragment extends ListFragment {
	
	private List<Question> questions;
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View layout = inflater.inflate(R.layout.list, container, false);
		
		Button evalButton = (Button) layout.findViewById(R.id.evaluateButton);
		
		evalButton.setOnClickListener(new OnClickListener() {
			@SuppressLint("ShowToast")
			@Override
			public void onClick(View v) {
				double points = checkCorrectAnswers();
				newPuntuationDialog(points).show();
			}
		});
		
		return layout;
	}
	
	private Dialog newPuntuationDialog(double points)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
     
        String title = "";
        if (points >= 0.5) 
        	title = "CONGRATULATIONS!";
        else
        	title = "UPS, YOU FAILED...";
        
        String message = "Your puntuation is: " + points * 10 + " points.";
        
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setPositiveButton("OK", new android.content.DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		});
     
        return builder.create();
    }

	protected double checkCorrectAnswers() {
		double nQuestions = questions.size();
		double rightAns = 0;
		
		String correctId;
		for (Question q : questions) {
			correctId = q.getCorrectAnswerId();
			for (Answer a : q.getAnswers()) {
				if (a.getId().equals(correctId) && a.isSelected())
					rightAns++;
				a.setSelected(false);
			}
		}
		
		return rightAns/nQuestions;
	}

	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		QuestionAdapter adapter = new QuestionAdapter(getActivity(), 0, QuestionsList.questions);
		questions = QuestionsList.questions;
		setListAdapter(adapter);
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		getListAdapter().getItem(position);
		QuestionsList a = (QuestionsList)getActivity();
		a.setSelectedQuestion(position);
		
		switchFragment();
	}
	
	private void switchFragment() {
		if (getActivity() == null)
			return;

		else if (getActivity() instanceof QuestionsList) {
			QuestionsList ra = (QuestionsList) getActivity();
			ra.switchContent();
		}
	}

//	// the meat of switching the above fragment
//	private void switchFragment(Fragment fragment) {
//		if (getActivity() == null)
//			return;
//
//		if (getActivity() instanceof QuestionsListActivity) {
//			QuestionsListActivity fca = (QuestionsListActivity) getActivity();
//			fca.switchContent(fragment);
//		}
//	}
	
//	public class SampleAdapter extends ArrayAdapter<SampleItem> {
//		public SampleAdapter(Context context) {
//			super(context, 0);
//		}
//		
//		public View getView(int position, View convertView, ViewGroup parent) {
//			if (convertView == null) {
//				convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, null);
//			}
//			ImageView icon = (ImageView) convertView.findViewById(R.id.row_icon);
//			icon.setImageResource(getItem(position).iconRes);
//			TextView title = (TextView) convertView.findViewById(R.id.row_title);
//			title.setText(getItem(position).tag);
//			
//			return convertView;
//		}
//	}
}
